package longrise.phone.com.bjjt_henan.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Adapter.CommentAdapter;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 评论列表列表
 */

public class CommentView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mListView;

    private CommentAdapter mAdapter;

    private String centerid;

    private List<EntityBean> dataLists;

    private static final int PAGE_SIZE = 10;

    private int pagenum = 1;

    private boolean isLoading = false;// 正在加载

    private TextView emptyIV;//无数据时的背景色

    public CommentView(Context context, String centerid) {
        super(context);
        this.mContext = context;
        this.centerid = centerid;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_comment, null);
        if (mView == null) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("评论");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        mListView = (PullToRefreshListView) mView.findViewById(R.id.comment_lv);
        initRefreshView();
        mAdapter = new CommentAdapter(mContext);
        mListView.setAdapter(mAdapter);
        emptyIV = (TextView) mView.findViewById(R.id.empty_tv);

        if (dataLists == null) {
            dataLists = new ArrayList<>();
        } else {
            dataLists.clear();
        }
    }

    private void initRefreshView() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        // 刚拉时的提示
        mListView.getLoadingLayoutProxy().setPullLabel("加载列表数据...");
        // 正在刷新提示
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");
        // 拉倒一定高度的提示
        mListView.getLoadingLayoutProxy().setReleaseLabel("放开以刷新...");
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    pagenum = 1;
                    loadData();
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    if (dataLists == null || dataLists.size() < 1) {
                        pagenum = 1;
                        loadData();
                        return;
                    }
                    loadData();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void refresh() {
        if (!isLoading) {
            DialogUtils.showDialog(mContext);
            pagenum = 1;
            loadData();
        }
    }

    private void loadData() {
        isLoading = true;
        BaseApplication mApplication = ((MainActivity) mContext).getAppData();
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        ApiImpI.getInstance(mContext).getCenterEvaluation(centerid, uid, token, pagenum, PAGE_SIZE, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                if (null != mListView) {// 停止刷新
                    mListView.onRefreshComplete();
                }
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
                isLoading = false;
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                if (null != mListView) {// 停止刷新
                    mListView.onRefreshComplete();
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean[] datas = bean.getBeans("data");
                    if (datas != null && datas.length > 0) {
                        if (pagenum == 1) {//重新刷新数据
                            dataLists.clear();
                        }
                        pagenum++;
                        for (EntityBean data : datas) {
                            dataLists.add(data);
                        }
                        if (null != mAdapter) {
                            mAdapter.setDatas(dataLists);
                        }
                    }
                } else {
                    String redes = bean.getString("redes");
                    UiUtil.showToast(mContext, redes);
                }
                if (null == dataLists || dataLists.size() <= 0) {
                    mListView.setEmptyView(emptyIV);
                } else {
                    emptyIV.setVisibility(View.GONE);//null
                }
                isLoading = false;
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (mListView != null) {
            mListView.setAdapter(null);
            mListView = null;
        }
        if (dataLists != null) {
            dataLists.clear();
            dataLists = null;
        }
        mView = null;
    }
}
